import {
  GetProductClassifyList,
  GetProductGroupingList
} from '@/api/onlineMan'

const params = {
  pageNum: 1,
  pageSize: 30
}

const com = {
  state: {
    productClassList: [],
    productGroupList: []
  },
  mutations: {
    SET_PRO_CLASS: (state, data) => {
      state.productClassList = data
    },
    SET_PRO_GROUP: (state, data) => {
      state.productGroupList = data
    }
  },
  actions: {
    getProClass: ({
      commit
    }, force = false) => {
      // force 是否强制重新获取商品分类数据
      if (force) {
        return new Promise((resolve, reject) => {
          GetProductClassifyList(params).then(res => {
            commit('SET_PRO_CLASS', res.data)
            resolve(true)
          }).catch(e => {
            reject(new Error(false))
          })
        })
      } else {
        commit('SET_PRO_CLASS', [])
      }
    },
    getProGroup: ({
      commit
    }, force = false) => {
      // force 是否强制重新获取商品分组数据
      if (force) {
        return new Promise((resolve, reject) => {
          GetProductGroupingList(params).then(res => {
            res.data.list.forEach(item => {
              item.value = item.id.toString()
              item.label = item.groupName
            })
            commit('SET_PRO_GROUP', res.data.list || [])
            resolve(true)
          }).catch(e => {
            reject(new Error(false))
          })
        })
      } else {
        commit('SET_PRO_GROUP', [])
      }
    }
  }
}

export default com
